extends Control

signal finished
onready var speaker_name = $Panel/VBoxContainer/speaker_name/speaker_name
onready var text = $Panel/VBoxContainer/text

var lines = []
var close_on_end = true

func _ready():
	pass # Replace with function body.
	
func _process(delta):
	if not visible: return
	if Input.is_action_just_pressed("ui_accept"):
		next_line()

func open(left, right, lines_arg):
	# TODO multiple sprites and speakers ,TODO move to per line
	var active_speakers = []
	if lines_arg.size() > 0:
		active_speakers = lines_arg[0].speakers
	for speaker in left:
		$speaker_sprite.set_speaker(speaker, speaker in active_speakers)
	for speaker in right:
		$speaker_sprite_r.set_speaker(speaker, speaker in active_speakers)
	self.visible = true
	lines = lines_arg
	next_line()
	pass

func next_line():
	if lines.empty():
		if close_on_end:
			self.visible = false
		emit_signal("finished")
		return
	var line = lines.pop_front()
	if line.speakers[0] == $speaker_sprite.speaker:
		$speaker_sprite.set_speaker($speaker_sprite.speaker, true, line.emote)
	if line.speakers[0] == $speaker_sprite_r.speaker:
		$speaker_sprite_r.set_speaker($speaker_sprite_r.speaker, true, line.emote)
#	print(line.speaker.emote_sprite())
	text.text = line.text
	speaker_name.text = line.speakers[0].full_name()
	
